package cn.web.web_module.service.impl;

import cn.itcast.mapper.adminMapper;
import cn.itcast.mapper.userMapper;
import cn.itcast.pojo.admin;
import cn.itcast.pojo.user;
import cn.itcast.util.ResponseEntity;
import cn.message.service.adminService;
import cn.web.web_module.service.LogOrRegisterService;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import cn.itcast.mapper.adminMapper;
import cn.message.service.adminService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 张松
 * @Description:
 * @Date: Created in 10:29 2020/7/11
 * @Modified By:
 */
@Service("LogOrRegisterService")
@Slf4j
public class LogOrRegisterServiceImpl implements LogOrRegisterService {


    @Resource
    private userMapper userMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private adminMapper adminMapper;

    @Resource
    private adminService adminService;


    @Override
    public void LogOrRegister(String Phone) {
        
    }

    /**
     *
     * @Description: 判断用户是否有手机号码
     *
     * @auther: 张松
     * @date: 20:52 2020/7/14
     * @param: [user]
     * @return: cn.itcast.pojo.user
     *
     */
    @Override
    public user selectUserIfHavePhone(user user) {
        user oneUser = userMapper.selectFindOneUser(user.getOpenId());
        return oneUser;
    }


    /**
     *
     * @Description: 用户绑定手机号码
     *
     * @auther: 张松
     * @date: 20:51 2020/7/14
     * @param: [user, code]
     * @return: java.lang.Integer
     *
     */
    @Override
    public Integer WXBindPhone(user user,String code) {
        String s = stringRedisTemplate.opsForValue().get(user.getUserPhone() + "code");
        if (s.equals(code)){
            int i = userMapper.updateByPrimaryKeySelective(user);
            stringRedisTemplate.opsForValue().set(user.getUserPhone() + "code",s+s+s,1800, TimeUnit.SECONDS);
            return i;
        }else {
            return 0;
        }


    }
    /**
     *
     * @Description: 判断该号码和code是否匹配
     * 
     * @auther: 张松
     * @date: 20:52 2020/7/14
     * @param: [phone, code]
     * @return: cn.itcast.pojo.admin
     *
     */
    @Override
    public ResponseEntity userLog(HttpServletRequest request, HttpServletResponse response, String phone, String code) {
        String s = stringRedisTemplate.opsForValue().get(phone + "code");
        if (s.equals(code)){
            admin admin = adminMapper.selectPhone(phone);
            HttpSession session = request.getSession();
            UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(admin.getAdminNickname(), admin.getAdminPassword());
            if (admin!=null){
                Subject subject = SecurityUtils.getSubject();
                try{
                    subject.login(usernamePasswordToken);
                    Map<String, Object> map1 = adminService.login(response, admin.getAdminNickname(), admin.getAdminPassword(), session);
                    stringRedisTemplate.opsForValue().set(phone + "code",s+s+s,180, TimeUnit.SECONDS);
                    return new ResponseEntity(200,"登入成功",map1);
                } catch (UnknownAccountException e) {
                    //e.printStackTrace();
                    //登录失败:用户名不存在
                    return new ResponseEntity(404,"用户不存在");
                }catch (IncorrectCredentialsException e) {
                    //e.printStackTrace();
                    //登录失败:密码错误
                    return new ResponseEntity(404,"密码错误");
                }catch (LockedAccountException e) {
                    return new ResponseEntity(911,"登录失败，该用户已被冻结");
                }
        }else{
                return new ResponseEntity("您的账号为注册,请联系管理员").error_300();
            }
        }
        return new ResponseEntity("验证码错误").error_300();
    }



    /**
     *
     * @Description: 查看手机号对应的管理员
     * 
     * @auther: 张松
     * @date: 10:52 2020/7/15
     * @param: [phone]
     * @return: cn.itcast.pojo.admin
     *
     */
    @Override
    public admin selectAdminPhone(String phone) {
        return adminMapper.selectPhone(phone);
    }

    /**
     *
     * @Description: 根据手机号码和验证码,管理员修改密码
     *
     * @auther: 张松
     * @date: 18:34 2020/7/15
     * @param: [phone, code, password]
     * @return: java.lang.Integer
     *
     */
    @Override
    public Integer updatePassword(String phone , String code,String password) {
        String s = stringRedisTemplate.opsForValue().get(phone + "code");
        if (!s.equals(code)){
            return 0;
        }
        admin admin = adminMapper.selectPhone(phone);
        admin.setAdminPassword(password);
        stringRedisTemplate.opsForValue().set(phone + "code",s+s+s,180, TimeUnit.SECONDS);
        return adminMapper.updateByPrimaryKeySelective(admin);
    }


}
